home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / sunbird / js / calEvent.js < prev    next >
Encoding:
JavaScript  |  2007-05-23  |  9.8 KB  |  305 lines

  1. /* -*- Mode: javascript; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Oracle Corporation code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  *  Oracle Corporation
  19.  * Portions created by the Initial Developer are Copyright (C) 2004
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
  24.  *   Mike Shaver <shaver@off.net>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. //
  41. // calEvent.js
  42. //
  43.  
  44. //
  45. // constructor
  46. //
  47. function calEvent() {
  48.     this.wrappedJSObject = this;
  49.     this.initItemBase();
  50.     this.initEvent();
  51.  
  52.     this.eventPromotedProps = {
  53.         "DTSTART": true,
  54.         "DTEND": true,
  55.         "DTSTAMP": true,
  56.         __proto__: this.itemBasePromotedProps
  57.     }
  58. }
  59.  
  60. // var trickery to suppress lib-as-component errors from loader
  61. var calItemBase;
  62.  
  63. var calEventClassInfo = {
  64.     getInterfaces: function (count) {
  65.         var ifaces = [
  66.             Components.interfaces.nsISupports,
  67.             Components.interfaces.calIItemBase,
  68.             Components.interfaces.calIEvent,
  69.             Components.interfaces.calIInternalShallowCopy,
  70.             Components.interfaces.nsIClassInfo
  71.         ];
  72.         count.value = ifaces.length;
  73.         return ifaces;
  74.     },
  75.  
  76.     getHelperForLanguage: function (language) {
  77.         return null;
  78.     },
  79.  
  80.     contractID: "@mozilla.org/calendar/event;1",
  81.     classDescription: "Calendar Event",
  82.     classID: Components.ID("{974339d5-ab86-4491-aaaf-2b2ca177c12b}"),
  83.     implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  84.     flags: 0
  85. };
  86.  
  87. calEvent.prototype = {
  88.     __proto__: calItemBase ? (new calItemBase()) : {},
  89.  
  90.     QueryInterface: function (aIID) {
  91.         if (aIID.equals(Components.interfaces.calIEvent) ||
  92.             aIID.equals(Components.interfaces.calIInternalShallowCopy))
  93.             return this;
  94.  
  95.         if (aIID.equals(Components.interfaces.nsIClassInfo))
  96.             return calEventClassInfo;
  97.  
  98.         return this.__proto__.__proto__.QueryInterface.call(this, aIID);
  99.     },
  100.  
  101.     cloneShallow: function (aNewParent) {
  102.         var m = new calEvent();
  103.         this.cloneItemBaseInto(m, aNewParent);
  104.  
  105.         return m;
  106.     },
  107.  
  108.     clone: function () {
  109.         var m;
  110.  
  111.         if (this.mParentItem) {
  112.             var clonedParent = this.mParentItem.clone();
  113.             m = clonedParent.recurrenceInfo.getOccurrenceFor (this.recurrenceId);
  114.         } else {
  115.             m = this.cloneShallow(null);
  116.         }
  117.  
  118.         return m;
  119.     },
  120.  
  121.     createProxy: function () {
  122.         if (this.mIsProxy) {
  123.             calDebug("Tried to create a proxy for an existing proxy!\n");
  124.             throw Components.results.NS_ERROR_UNEXPECTED;
  125.         }
  126.  
  127.         var m = new calEvent();
  128.         m.initializeProxy(this);
  129.  
  130.         return m;
  131.     },
  132.  
  133.     makeImmutable: function () {
  134.         this.makeItemBaseImmutable();
  135.     },
  136.  
  137.     initEvent: function() {
  138.         this.startDate = new CalDateTime();
  139.         this.endDate = new CalDateTime();
  140.     },
  141.  
  142.     get duration() {
  143.         return this.endDate.subtractDate(this.startDate);
  144.     },
  145.  
  146.     get recurrenceStartDate() {
  147.         return this.startDate;
  148.     },
  149.  
  150.     icsEventPropMap: [
  151.     { cal: "DTSTART", ics: "startTime" },
  152.     { cal: "DTEND", ics: "endTime" }],
  153.  
  154.     set icalString(value) {
  155.         this.icalComponent = icalFromString(value);
  156.     },
  157.  
  158.     get icalString() {
  159.         const icssvc = Components.
  160.           classes["@mozilla.org/calendar/ics-service;1"].
  161.           getService(Components.interfaces.calIICSService);
  162.         var calcomp = icssvc.createIcalComponent("VCALENDAR");
  163.         calcomp.prodid = "-//Mozilla Calendar//NONSGML Sunbird//EN";
  164.         calcomp.version = "2.0";
  165.         if (this.hasProperty("METHOD")) {
  166.             calcomp.method = this.getProperty("METHOD");
  167.         }
  168.         calcomp.addSubcomponent(this.icalComponent);
  169.         return calcomp.serializeToICS();
  170.     },
  171.  
  172.     get icalComponent() {
  173.         const icssvc = Components.
  174.           classes["@mozilla.org/calendar/ics-service;1"].
  175.           getService(Components.interfaces.calIICSService);
  176.         var icalcomp = icssvc.createIcalComponent("VEVENT");
  177.         this.fillIcalComponentFromBase(icalcomp);
  178.         this.mapPropsToICS(icalcomp, this.icsEventPropMap);
  179.         
  180.         var bagenum = this.mProperties.enumerator;
  181.         while (bagenum.hasMoreElements()) {
  182.             var iprop = bagenum.getNext().
  183.                 QueryInterface(Components.interfaces.nsIProperty);
  184.             try {
  185.                 if (!this.eventPromotedProps[iprop.name]) {
  186.                     var icalprop = icssvc.createIcalProperty(iprop.name);
  187.                     icalprop.value = iprop.value;
  188.                     var propBucket = this.mPropertyParams[iprop.name];
  189.                     if (propBucket) {
  190.                         for (paramName in propBucket) {
  191.                             icalprop.setParameter(paramName,
  192.                                                   propBucket[paramName]);
  193.                         }
  194.                     }
  195.                     icalcomp.addProperty(icalprop);
  196.                 }
  197.             } catch (e) {
  198.                 dump("XXX failed to set " + iprop.name + " to " + iprop.value +
  199.                 ": " + e + "\n");
  200.             }
  201.         }
  202.         return icalcomp;
  203.     },
  204.  
  205.     eventPromotedProps: null,
  206.  
  207.     set icalComponent(event) {
  208.         this.modify();
  209.         if (event.componentType != "VEVENT") {
  210.             event = event.getFirstSubcomponent("VEVENT");
  211.             if (!event)
  212.                 throw Components.results.NS_ERROR_INVALID_ARG;
  213.         }
  214.  
  215.         this.setItemBaseFromICS(event);
  216.         this.mapPropsFromICS(event, this.icsEventPropMap);
  217.  
  218.         this.importUnpromotedProperties(event, this.eventPromotedProps);
  219.         
  220.         // If there is a duration set on the event, calculate the right
  221.         // end time.
  222.         // XXX This means that serializing later will loose the duration
  223.         //     information, to replace it with a dtend. bug 317786
  224.         if (event.duration) {
  225.             this.endDate = this.startDate.clone();
  226.             this.endDate.addDuration(event.duration);
  227.         }
  228.         
  229.         // If endDate is still invalid neither a end time nor a duration is set
  230.         // on the event. We have to set endDate ourselves.
  231.         // If the start time is a date-time the event ends on the same calendar
  232.         // date and time of day. If the start time is a date the events
  233.         // non-inclusive end is the end of the calendar date.
  234.         if (!this.endDate.isValid) {
  235.             this.endDate = this.startDate.clone();
  236.             if (this.startDate.isDate) {
  237.                 this.endDate.day += 1;
  238.                 this.endDate.normalize();
  239.             }
  240.         }
  241.         
  242.         // Importing didn't really change anything
  243.         this.mDirty = false;
  244.     },
  245.  
  246.     isPropertyPromoted: function (name) {
  247.         return (this.eventPromotedProps[name]);
  248.     },
  249.  
  250.     getOccurrencesBetween: function(aStartDate, aEndDate, aCount) {
  251.         if (this.recurrenceInfo) {
  252.             return this.recurrenceInfo.getOccurrences(aStartDate, aEndDate, 0, aCount);
  253.         }
  254.  
  255.         var start = ensureDateTime(this.startDate);
  256.         var end = ensureDateTime(this.endDate);
  257.         
  258.         var queryStart = ensureDateTime(aStartDate);
  259.         var queryEnd = ensureDateTime(aEndDate);
  260.  
  261.         var isZeroLength = !start.compare(end);
  262.         if ((isZeroLength &&
  263.              start.compare(queryStart) >= 0 &&
  264.              start.compare(queryEnd) < 0) ||
  265.             (!isZeroLength &&
  266.              start.compare(queryEnd) < 0 &&
  267.              end.compare(queryStart) > 0)) {
  268.             
  269.           aCount.value = 1;
  270.           return ([ this ]);
  271.         }
  272.  
  273.         aCount.value = 0;
  274.         return null;
  275.     },
  276.  
  277.     set startDate(value) {
  278.         this.modify();
  279.  
  280.         // We're about to change the start date of an item which probably
  281.         // could break the associated calIRecurrenceInfo. We're calling
  282.         // the appropriate method here to adjust the internal structure in
  283.         // order to free clients from worrying about such details.
  284.         if (this.parentItem == this) {
  285.             var rec = this.recurrenceInfo;
  286.             if (rec) {
  287.                 rec.onStartDateChange(value,this.startDate);
  288.             }
  289.         }
  290.  
  291.         this.setProperty("DTSTART", value);
  292.     },
  293.  
  294.     get startDate() {
  295.         return this.getProperty("DTSTART");
  296.     }
  297. };
  298.  
  299. // var to avoid spurious errors when loaded as component during autoreg
  300.  
  301. var makeMemberAttr;
  302. if (makeMemberAttr) {
  303.     makeMemberAttr(calEvent, "DTEND", null, "endDate", true);
  304. }
  305.